//
//  FQDHeadlineFeedCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/12/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDHeadlineFeedCell.h"
#import "FQDCalendarIcon.h"
#import "FQDHeadlineContentView.h"
#import "FQDAppearHistory.h"
#import "FQDHeadlineFeedCellItem.h"
#import "FQDRestModel.h"

#import "Masonry.h"

@interface FQDHeadlineFeedCell ()

// 圆角背景
@property (nonatomic, strong) UIView *bgView;
// 日期图标
@property (nonatomic, strong) FQDCalendarIcon *calIcon;
// 顶部标题
@property (nonatomic, strong) UILabel *titleLabel;
// 查看详情
@property (nonatomic, strong) UILabel *showDetailLabel;
// 小标题内容列表
@property (nonatomic, strong) NSMutableArray *contentList;

@end

@implementation FQDHeadlineFeedCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _contentList = [NSMutableArray array];
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    self.backgroundColor = RGB(244, 244, 244);
    
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.shadowOffset = CGSizeZero;
    _bgView.layer.shadowOpacity = 0.9f;
    _bgView.layer.shadowColor = RGB(222, 222, 222).CGColor;
    _bgView.layer.cornerRadius = 5.0f;
    [self.contentView addSubview:_bgView];
    
    _calIcon = [[FQDCalendarIcon alloc] init];
    [_bgView addSubview:_calIcon];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(17.0f);
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.text = @"大公司头条";
    [_bgView addSubview:_titleLabel];
    
    _showDetailLabel = [[UILabel alloc] init];
    _showDetailLabel.font = FQDRegularFontWithSize(12.0f);
    _showDetailLabel.textColor = RGB(152, 152, 152);
    _showDetailLabel.text = @"查看详情>>";
    _showDetailLabel.textAlignment = NSTextAlignmentRight;
    [_bgView addSubview:_showDetailLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
//    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(weakSelf);
//        make.height.equalTo(@(313.5));
//    }];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView).offset(5);
        make.bottom.and.right.equalTo(weakSelf.contentView).offset(-5);
    }];
    
    [_calIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.bgView).offset(7);
        make.size.equalTo(@25);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.calIcon.mas_right).offset(7);
        make.right.equalTo(weakSelf.bgView).offset(-7);
        make.centerY.equalTo(weakSelf.calIcon);
        make.height.equalTo(weakSelf.calIcon);
    }];
    
}

- (void)addContentFromList:(NSArray *)detailList
{
    WeakSelf;
    NSInteger prevDetailCount = [self.contentList count];
    NSInteger currDetailCount = [detailList count];
    
    NSInteger remainCount = currDetailCount - prevDetailCount;
    
    
    if (remainCount > 0) {
        for (NSInteger i = 0; i < remainCount; i++) {
            FQDHeadlineContentView *cv = [FQDHeadlineContentView new];
            cv.translatesAutoresizingMaskIntoConstraints = NO;
            [self.bgView addSubview:cv];
            [self.contentList addObject:cv];
        }
    } else if (remainCount < 0) {
        [self.contentList removeObjectsInRange:NSMakeRange(currDetailCount, -remainCount)];
    }
    
    
    UIView *lastView = self.titleLabel;
    for (NSInteger i = 0; i < currDetailCount; i++) {
        FQDHeadlineBriefInfo *breifInfo = detailList[i];
        FQDHeadlineContentView *cv = self.contentList[i];
        cv.content = breifInfo.desc;
        
        CGFloat offsetY = (i == 0) ? 15.0f : 0;
        [cv mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView.mas_bottom).offset(offsetY);
            make.left.and.right.equalTo(weakSelf.bgView);
        }];
        [cv layoutIfNeeded];
        lastView = cv;
    }
    
    [_showDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(5.5);
        make.right.equalTo(weakSelf.bgView).offset(-10);
        make.bottom.equalTo(weakSelf.bgView).offset(-18);
        make.width.equalTo(weakSelf.bgView).offset(-10);
    }];
}

- (void)configureCellWithCellItem:(FQDHeadlineFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDHeadline *headline = cellItem.headline;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:headline.post.publish_time];
    [self.calIcon showDate:date];
    [self addContentFromList:headline.detailList];
    if (!headline.hasShowed) {
        [[FQDAppearHistory sharedHistory] increaseAppearCountForQdID:headline.post.ID genre:headline.post.genre];
        headline.hasShowed = YES;
    }
}

+ (NSString *)reuseId
{
    return @"FQDHeadlineFeedCell";
}

@end
